import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {AppService} from "../../../services/app.service";
import {PageParam} from "../../../models/page-param-model";
import {ResDataModel} from "../../../models/res-data-model";

/**
 * Generated class for the UserListDemoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-user-list-demo',
  templateUrl: 'user-list-demo.html',
})
export class UserListDemoPage {

  hasmore = true;
  users: any;
  selectedItem: any;

  spinner1: boolean = true;

  pageParam = new PageParam;

  postData = {
    userName:'',
    ancestorSfjgDm:'',
  };

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl: ViewController,
              public appService: AppService) {
    this.selectedItem = this.navParams.get("item");
    //this.params.favoritesId = this.selectedItem.FavoritesId;
  }

  ionViewDidLoad() {
    //this.getFavoritesItems();
    this.getUsers();
    console.log('ionViewDidLoad ProductListPage');
  }

  getUsers(){

    this.appService.httpPost('AppUserCtrl_search',this.postData,this.pageParam,(res:ResDataModel) =>{
      this.users = res.datas[0].trs;
      this.pageParam.pageNum += 1;
      //console.log(this.users);
    })
  }

  doInfinite(infiniteScroll) {
    if (this.hasmore == false) {
      infiniteScroll.complete();
      return;
    }

    this.appService.httpPost('AppUserCtrl_search',this.postData,this.pageParam,(res:ResDataModel) =>{
      if(res.datas.length > 0) {
        //this.users = data[0].trs;
        this.users = this.users.concat(res.datas[0].trs);
        this.pageParam.pageNum += 1;
      } else {
        this.hasmore = false;
        console.log("没有数据啦！")
      }
      infiniteScroll.complete();
    })
  }

  goDetails(item,type) {
    console.debug('go details:' + type);
    this.navCtrl.push('UserDetailsPage', { item: item ,type: type});
  }

  dismiss(){
    this.viewCtrl.dismiss();
  }
}
